﻿/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 **/
using System;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Peter.Ftp.Data;
using Peter.Ftp.DataAccess;
using Peter.Ftp.Tests.DataAccess;

namespace Peter.Ftp.Tests.Data
{
   /// <summary>
   /// Test cases for the FtpItem class.
   /// </summary>
   [TestClass]
   public class FtpItemTests
   {
      /// <summary>
      /// Verifies that the ftp connection is not null for a new ftp item.
      /// </summary>
      [TestMethod]
      [ExpectedException (typeof (ArgumentNullException))]
      public void TestNullConnection ()
      {
         new FtpItem (false, null, "TestItem");
      }

      /// <summary>
      /// Verifies that the ftp connection is connected.
      /// </summary>
      [TestMethod]
      [ExpectedException (typeof (InvalidOperationException))]
      public void TestClosedConnection ()
      {
         new FtpItem (false, new FtpConnection (), "TestItem");
      }

      /// <summary>
      /// Verifies the constructor data.
      /// </summary>
      [TestMethod]
      public async Task TestConstructor ()
      {
         using (var connection = new FtpConnection ())
         {
            await connection.Connect (FtpConnectionTests.ConnectionData);
            var item = new FtpItem (false, connection, "TestItem");
            Assert.AreEqual (connection, item.Connection,
               "FtpItem did not set the connection via the constructor.");
         }
      }

      /// <summary>
      /// Verifies the properties.
      /// </summary>
      [TestMethod]
      public async Task TestProperties ()
      {
         using (var connection = new FtpConnection ())
         {
            await connection.Connect (FtpConnectionTests.ConnectionData);
            var root = new FtpItem (false, connection, "TestRoot");
            Assert.IsTrue (root.IsRoot, "Root FtpItem should be flagged as root.");
            Assert.IsFalse (root.DynamicLoad, "Root FtpItem should not be loaded dynamically.");
            Assert.IsFalse (root.IsDirectory, "Root FtpItem should not be flagged as a directory.");

            var folder = new FtpItem (true, connection, "TestFolder -> Test_Folder", root);
            root.Children.Add (folder);
            Assert.AreEqual ("/TestFolder/", folder.GetDirectory (), "FtpItem Directory is different than expected.");
            Assert.AreEqual ("TestFolder", folder.GetText (), "FtpItem Text is different than expected.");
            Assert.IsFalse (folder.IsRoot, "Directory FtpItem should not be flagged as a root.");
            Assert.IsTrue (folder.DynamicLoad, "Directory FtpItem should be dynamically loaded.");
            Assert.IsTrue (folder.IsDirectory, "Directory FtpItem should be flagged as a directory.");

            var file = new FtpItem (false, connection, "TestFile", folder);
            folder.Children.Add (file);
            Assert.IsFalse (file.IsRoot, "File FtpItem should not be flagged as a root.");
            Assert.IsFalse (file.DynamicLoad, "File FtpItem should not be loaded dynamically.");
            Assert.IsFalse (file.IsDirectory, "Root FtpItem should not be flagged as a directory.");
            Assert.AreEqual ("TestFile", file.GetText (), "FtpItem Text is different than expected.");

            var file2 = new FtpItem (false, connection, "TestFile2", folder);
            folder.Children.Add (file2);
            Assert.AreEqual ("TestFile2", file2.GetText (), "FtpItem Text is different than expected.");
            Assert.AreEqual ("/TestFolder/", file2.GetDirectory (), "FtpItem Directory is different than expected.");
         }
      }
   }
}
